package org.example.service;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import okhttp3.RequestBody;
import org.example.utils.Md5Utils;
import org.example.utils.OkHttp3Utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AvatarToken {

    public String getToken2(String url,String appid,String appKey) throws IOException {
        String timestamp = System.currentTimeMillis()+"";
        String version = "v1";
        String model = "remote";
        JSONObject requestJson = new JSONObject();
        JSONObject baseJson = new JSONObject();
        baseJson.put("appId",appid);
        baseJson.put("version",version);
        baseJson.put("timestamp",timestamp);
        requestJson.put("base",baseJson);
        requestJson.put("model",model);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(Md5Utils.to32LowerCase(appKey+timestamp));
        stringBuilder.append(requestJson.toJSONString());
        String authorization = Md5Utils.to32LowerCase(stringBuilder.toString());
        Map<String,String> httpHeadMap = new HashMap<>();
        httpHeadMap.put("Authorization",authorization);
        httpHeadMap.put("Content-Type","application/json");
        httpHeadMap.put("Connection","keep-alive");
        String res = OkHttp3Utils._post(url,RequestBody.create(OkHttp3Utils.JSON, requestJson.toJSONString()),httpHeadMap);
        System.out.println("res:"+res);
        ResToken resToken = JSONObject.parseObject(res,ResToken.class);
        if (resToken.retcode.equals("000000")){
            return resToken.accesstoken;
        }
        return "";
    }

    public String getToken(String url,String appid,String secretKey) throws IOException {
        ReqToken rt = new ReqToken();
        rt.model = "remote";
        Base base = new Base();
        base.appid = appid;
        base.version = "v1";
        base.timestamp = System.currentTimeMillis()+"";
        rt.base = base;
        String rtJson = JSONObject.toJSONString(rt);
        System.out.println("rtJson:"+rtJson);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(stringBuilder);
        stringBuilder.append(rtJson);
        String authorization = Md5Utils.to32LowerCase(stringBuilder.toString());

        RequestBody body = RequestBody.create(OkHttp3Utils.JSON, rtJson);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type","application/json");
//        headers.put("Authorization",getSign(secretKey,base.timestamp,rtJson.getBytes()));
        headers.put("Authorization",authorization);
        System.out.println("url:"+url+"\nbody:"+body+"\nheaders:"+headers);
        String res = OkHttp3Utils._post(url,body,headers);
        ResToken resToken = JSONObject.parseObject(res,ResToken.class);
        System.out.println("retcode:"+resToken.retcode);
        System.out.println("token:"+resToken.accesstoken);
        if (resToken.retcode.equals("000000")){
            return resToken.accesstoken;
        }
        return "";
    }


    private String getSign(String secretkey,String timestamp, byte[] reqBytes) {
        StringBuilder stringBuilder = new StringBuilder();
        String sk = Md5Utils.to32LowerCase(secretkey + timestamp);
        System.out.println("sk："+sk);
        byte[] skBytes = sk.getBytes();
        for (int i = 0; i < skBytes.length; i++) {
            stringBuilder.append(skBytes[i]);
        }
        for (int i = 0; i < reqBytes.length; i++) {
            stringBuilder.append(reqBytes[i]);
        }
        System.out.println("stringBuilder.toString()："+stringBuilder.toString());
//        byte[] encodeBytes = new byte[skBytes.length + reqBytes.length];
//        System.arraycopy(skBytes, 0, encodeBytes, 0, skBytes.length);
//        System.arraycopy(reqBytes, 0, encodeBytes, skBytes.length, reqBytes.length);
//
//        System.out.println("encodeBytes：");
//        for (int i = 0; i < encodeBytes.length; i++) {
//            System.out.print(encodeBytes[i]);
//        }

        String sign = Md5Utils.to32LowerCase(stringBuilder.toString());
        System.out.println("sign:"+sign);
        return sign;
    }



}
@Data
class Base{
    String appid;
    String version;
    String timestamp;
}
@Data
class ReqToken {
    Base  base;
    String model;
}
@Data
class ResToken {
    String accesstoken;
    String expiresin;
    String retcode;
}
